<template>
  <div>
    <el-dialog title="外部链接配置"
               :visible.sync="dialogExternialLink"
               width="50%"
               :before-close="handleClose">
      <el-row :gutter="20">
        <el-form :model="requestParams"
                 :rules="rules"
                 ref="requestParams"
                 label-width="80px">
          <el-col :span="24">
            <el-form-item label="页面链接"
                          prop="url">
              <el-input v-model="requestParams.url"
                        placeholder="请输入页面链接"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="帐号"
                          prop="user_name">
              <el-input v-model="requestParams.user_name"
                        placeholder="请输入帐号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="密码"
                          prop="user_pw">
              <el-input v-model="requestParams.user_pw"
                        type="password"
                        placeholder="请输入密码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24"
                  class="bottom-box">
            <el-button @click="onCancel">取消</el-button>
            <el-button @click="onSure('requestParams')"
                       type="primary">确定</el-button>
          </el-col>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { CommonCheckApi } from "@/services/SystemSetting";
var checkPwd = (rule, value, cb) => {
  //验证密码的正则表达式
  const regPwd =
    /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*)(?=.*[`~!@#$%^&*()_\-+=<>.?:"{}].*).{10,32}$/
  if (regPwd.test(value)) {
    return cb()
  }
  cb(new Error('密码需包含大小写字母、数字及特殊字符，长度在10-32位之间'))
}
export default {
  props: {
    objRow: {
      type: Object,
      default: () => {
        return {}
      }
    },
    dialogExternialLink: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  data () {
    return {
      rules: {
        url: [
          { required: true, message: '页面链接不能为空', trigger: ['blur', 'change'] }//外部链接校验
        ],
        user_name: [
          { required: true, message: '帐号不能为空', trigger: ['blur', 'change'] }//帐号
        ],
        user_pw: [
          { required: true, message: '密码不能为空', trigger: ['blur', 'change'] },//密码          
          { validator: checkPwd, trigger: 'blur' },
        ]
      },
      requestParams: {
        url: '',//外部链接
        user_name: '',//帐号
        user_pw: '',//密码
      }
    }
  },
  methods: {
    getModulList (cTplId) {//模块列表
      CommonCheckApi.getBusinessConfigDetailAPI({
        params: {
          check_tpl_id: cTplId,//业务类型的主键值
        }
      }).then((res) => {
        if (res.code == 0) {
          let list = res.data.list;
          if (list.length > 0) {
            this.requestParams = list[0];//默认第一个
          }
          console.log('详情 res=', res)
          // this.onCancel();//取消
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    handleClose (done) {//关闭
      this.onCancel();//取消
      done()
    },
    onCancel () {//取消
      this.$emit('update:objRow', {});//重置
      this.$emit('update:dialogExternialLink', false);//更新为关闭
      Object.assign(this.$data, this.$options.data())//重置
      this.$refs.requestParams.resetFields();//表单重置
    },
    getSaveParams () {//获取保存的参数
      let obj = {
        check_tpl_id: this.objRow._id,//主键值
        business_type: this.objRow.business_type,//类型
        tpl_config: [
          this.requestParams
        ]
      };
      return obj;
    },
    onSave () {//保存
      CommonCheckApi.saveBusinessConfigAPI({
        data: this.getSaveParams()//获取保存的参数
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success('操作成功')
          this.onCancel();//取消
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    onSure (data) {//确定
      this.$refs[data].validate((valid) => {
        if (valid) {
          this.onSave();//保存
        } else {
          return false;
        }
      })
    }
  },
  watch: {//监听
    dialogExternialLink (n, o) {
      if (n == true) {
        this.getModulList(this.objRow._id);//业务类型的主键值
      }
      console.log('w dialogExternialLink=', n, this.objRow)
    }
  },
  created () {
    if (this.dialogExternialLink == true) {
      this.getModulList(this.objRow._id);//业务类型的主键值
    }
    console.log('c dialogExternialLink=', this.dialogExternialLink, this.objRow)
  }
}
</script>

<style lang="scss" scoped>
.bottom-box {
  margin-top: 20px;
  text-align: right;
}
</style>